package nimble.net;

import org.apache.commons.codec.CharEncoding;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpUtils {

    private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);

    private static TrustManager truseAllManager = new X509TrustManager() {
        public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    public static byte[] getBytes(String url) throws Exception {
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("User-Agent","Fiddler");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity entityResponse = httpResponse.getEntity();
        if (entityResponse == null) {
            return null;
        }
        return EntityUtils.toByteArray(entityResponse);
    }

    public static String getString(String url) throws Exception {
        CloseableHttpClient httpClient = null;
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        }).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        try {
            httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("User-Agent","Fiddler");
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity entityResponse = httpResponse.getEntity();
            if (entityResponse == null) {
                return null;
            }
            return EntityUtils.toString(entityResponse);
        } finally {
            httpClient.close();
        }
    }

    public static byte[] post(String url, byte[] data) throws Exception {
        CloseableHttpClient httpClient = null;
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        }).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        try {
            httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            //
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new ByteArrayEntity(data));
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity entityResponse = httpResponse.getEntity();
            if (entityResponse == null) {
                return null;
            }
            return EntityUtils.toByteArray(entityResponse);
        } finally {
            httpClient.close();
        }
    }
    public static byte[] post(String url, byte[] data,String proxyHost,int proxyPort) throws Exception {
        CloseableHttpClient httpClient = null;
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        }).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        try {
            HttpHost  proxy = new HttpHost(proxyHost,proxyPort);
            httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
                    .setProxy(proxy)
                    .build();
            //
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new ByteArrayEntity(data));
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity entityResponse = httpResponse.getEntity();
            if (entityResponse == null) {
                return null;
            }
            return EntityUtils.toByteArray(entityResponse);
        } finally {
            httpClient.close();
        }
    }

    public static String post(String url, String data) throws Exception {
        byte[] responseBytes = post(url, data.getBytes(CharEncoding.UTF_8));
        return new String(responseBytes, CharEncoding.UTF_8);
    }
}
